package com.aiti.lulian.controller;

import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardTemplateBackgroundDto;
import com.aiti.lulian.entity.CardTemplateBackground;
import com.aiti.lulian.service.CardTemplateBackgroundService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cardTemplateBackground")
public class CardTemplateBackgroundController extends WebController {

    @Autowired
    private CardTemplateBackgroundService cardTemplateBackgroundService;

    @PostMapping
    public Message save(@RequestBody CardTemplateBackground entity) {
        if(StringUtils.isEmpty(entity.getCreator())){
            entity.setCreator(findUserId());
        }
        return cardTemplateBackgroundService.saveBackground(entity);
    }

    @PutMapping
    public Message update(@RequestBody CardTemplateBackground entity) {
        entity.setUpdateBy(findUserId());
        return cardTemplateBackgroundService.updateBackground(entity);
    }

    @DeleteMapping("/delete")
    public Message delete(@RequestParam String baseId) {
        return cardTemplateBackgroundService.deleteBackground(baseId);
    }

    @GetMapping("/{baseId}")
    public Message detail(@PathVariable String baseId) {
        return cardTemplateBackgroundService.detail(baseId);
    }



    @GetMapping("/list")
    public Message list(CardTemplateBackgroundDto dto) {
        Page page = startPage(dto);
        return cardTemplateBackgroundService.backgroundList(page, dto);
    }

    @PostMapping("/saveBatch")
    public Message save(@RequestBody CardTemplateBackgroundDto dto) {
        List<CardTemplateBackground> backgroundList = dto.getBackgroundList();
        if(!CollectionUtils.isEmpty(backgroundList)){
            return cardTemplateBackgroundService.saveBackgroundBatch(backgroundList);
        }else{
            return Message.success();
        }


    }

    @PostMapping("/savePre")
    public Message savePre(@RequestBody CardTemplateBackground entity) {
        entity.setCreator(findUserId());
        return cardTemplateBackgroundService.savePreBackground(entity);
    }

    @GetMapping("/preList")
    public Message preList(CardTemplateBackgroundDto dto) {
        Page page = startPage(dto);
        return cardTemplateBackgroundService.preBackgroundList(page, dto);
    }


}